#include "common/config.hpp"

#include <fstream>
#include <sstream>
#include <iostream>


std::string Config::DB_HOST = "localhost";
std::string Config::DB_USER = "diabio";
std::string Config::DB_PASSWORD = "diabio";
std::string Config::DB_NAME = "web_cloud_storage_service";
int Config::SERVER_PORT = 8080;
std::string Config::UPLOAD_PATH = "./static/uploads/";

// 初始化OSS静态变量
std::string Config::OSS_ENDPOINT = "";
std::string Config::OSS_BUCKET_NAME = "";

// RabbitMQ
std::string Config::UPLOAD_TEMP_PATH = "";
std::string Config::RABBITMQ_URI = "";
std::string Config::RABBITMQ_EXCHANGE = "";
std::string Config::RABBITMQ_QUEUE = "";
std::string Config::RABBITMQ_ROUTING_KEY = "";

bool Config::load(const std::string& configFile) {
    std::ifstream file(configFile);
    if (!file.is_open()) {
        return false;
    }

    std::string line;
    while (std::getline(file, line)) {
        if (line.empty() || line[0] == '#') continue;

        size_t pos = line.find("=");
        if (pos != std::string::npos) {
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + 1);

            if (key == "db.host") DB_HOST = value;
            else if (key == "db.user") DB_USER = value;
            else if (key == "db.password") DB_PASSWORD = value;
            else if (key == "db.name") DB_NAME = value;
            else if (key == "server.port") SERVER_PORT = std::stoi(value);

            // 加载OSS配置
            else if (key == "oss.endpoint") OSS_ENDPOINT = value;
            else if (key == "oss.bucket") {
                OSS_BUCKET_NAME = value;
            }

            // 加载临时上传目录
            else if (key == "upload.temppath") UPLOAD_TEMP_PATH = value;
            
            // 加载RabbitMQ配置
            else if (key == "rabbitmq.uri") RABBITMQ_URI = value;
            else if (key == "rabbitmq.exchange") RABBITMQ_EXCHANGE = value;
            else if (key == "rabbitmq.queue") RABBITMQ_QUEUE = value;
            else if (key == "rabbitmq.routingkey") RABBITMQ_ROUTING_KEY = value;
        }
    }
    return true;
}
